package middleware

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

// 跨域
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		cors.New(cors.Config{
			// 允许所以域名
			//AllowAllOrigins: true,
			// 也可以指定域名
			AllowOrigins: []string{"*"},
			AllowMethods: []string{"*"},
			AllowHeaders: []string{"Origin"},
			ExposeHeaders: []string{"Content-Length", "Authorization"},

			// 是否发送cookie请求
			//AllowCredentials: true,

			// 如果域名不对会提示指定路由
			//AllowOriginFunc: func(origin string) bool {
			//	return origin == "https://www.baidu.com"
			//},

			// 预请求
			MaxAge: 12 * time.Hour,
		})
	}
}